﻿using System;
using System.Collections.Generic;
using System.Text;
using RouterControl.Whs.ErrorReporting;

namespace RouterControl.Service
{
    public sealed class ErrorReporting
    {
        private static readonly ErrorReporting _instance = new ErrorReporting();

        public static ErrorReporting Instance
        {
            get { return _instance; }
        }

        private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof(ErrorReporting));
        private int _lastSubmittedErrorIndex = -1;

        public int SubmittedErrors
        {
            get;
            private set;
        }

        public bool IsSubmitting
        {
            get;
            private set;
        }

        public float SubmittingProgress
        {
            get;
            private set;
        }

        public int UnsubmittedErrors
        {
            get
            {
                return Whs.ErrorReporting.ErrorReportingSingleton.Instance.GetUnsubmittedErrorCount();
            }
        }

        private ErrorReporting()
		{
			if (_log.IsDebugEnabled)
				_log.Debug("Creating singleton");

			try
			{
				// load state
				try
				{
                    this.SubmittedErrors = Properties.States.Default.SubmittedErrors;
				}
				catch
				{
                    _log.Warn("Failed to read SubmittedErrors state");

                    this.SubmittedErrors = 0;
				}

                this.IsSubmitting = false;
                this.SubmittingProgress = 1.0f;

                // register with error reporting
                ErrorReportingSingleton.Instance.OnErrorSubmitted += 
                    new ErrorReportingSingleton.OnErrorSubmittedDelegate(OnErrorSubmitted);
	
				// start error submitting
                if (Properties.Settings.Default.EnableErrorReportsSubmission2)
                    ErrorReportingSingleton.Instance.StartSubmittingErrors();
			}
			catch (Exception e)
			{
				_log.Fatal("Failed to create singleton", e);

				throw;
			}

		}

        private void OnErrorSubmitted(ErrorReportingSingleton.ErrorInfo error, int index, int totalErrorCount)
        {
            // handle submitting start state
            if (index <= 0)
            {
                this.IsSubmitting = true;
                this.SubmittingProgress = 0.0f;

                return;
            }

            // increase counter
            if (_lastSubmittedErrorIndex != index)
            {
                this.SubmittedErrors++;

                _lastSubmittedErrorIndex = index;

                // set percentage
                this.IsSubmitting = true;
                this.SubmittingProgress = ((float)index / totalErrorCount);

                // save state
                Properties.States.Default.SubmittedErrors = this.SubmittedErrors;
                Properties.States.Default.Save();
            }

            // handle completed state
            if (index >= totalErrorCount)
            {
                this.IsSubmitting = false;
                this.SubmittingProgress = 1.0f;
            }
        }
    }
}
